<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <ul class="nav">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
    </ul>

    <script>
      function fn() {
        var num = 10;
        function fun() {
          console.log(num);
        }
        return fun;
      }
      var f = fn();
      f();
      //循环输出点击每个li的索引
      const lis = document.querySelector(".nav").querySelectorAll("li");
      /*  for (var i = 0; i < lis.length; i++) {
         lis[i].index = i; 
        lis[i].onclick = function() {
          console.log(this.index); 
        };
      } */

      //利用闭包
      for (var i = 0; i < lis.length; i++) {
        (function(i) {
          lis[i].onclick = function() {
            console.log(lis[i].innerText);
          };
        })(i);
      }
      //let
      for (let i = 0; i < lis.length; i++) {
        lis[i].onclick = function() {
          console.log(i);
        };
      }

      //闭包应用 3秒后 打印所有的li元素内容
      for (var i = 0; i < lis.length; i++) {
        (function(i) {
          setTimeout(function() {
            console.log(lis[i].innerHTML);
          }, 3000);
        })(i);
      }
      //闭包应用-计算打车价格
      //打车起步价13（3公里内） 之后每多一公里增加5块 用户输入公里数就可以计算打车价格
      //如果有堵车 总价格多收10块

      var car = (function() {
        const startPrice = 13;
        let totalPrice = 0;
        return {
          normalTotalPrice: function(n) {
            if (n <= 3) {
              totalPrice = startPrice;
            } else {
              totalPrice = startPrice + (n - 3) * 5;
            }
            return totalPrice;
          },
          congestionTotalPrice: function(flag) {
            return flag ? totalPrice + 10 : totalPrice;
          }
        };
      })();
      console.log(car.normalTotalPrice(5));
      console.log(car.congestionTotalPrice(true));
      //利用递归函数求1-n的阶乘函数
      function digui(num) {
        if (num == 1) {
          return 1;
        }
        return num * digui(num - 1);
      }
      console.log(digui(3));

      //利用递归函数求 斐波那契数列  1,1,2,3,5,8,13,21
      //输入数字n  n对应的序列值
      function fb(n) {
        if (n === 1 || n === 2) {
          return 1;
        }
        return fb(n - 1) + fb(n - 2);
      }
      console.log(fb(6));
    </script>
    <script>
      // 递归 根据id返回对应数据
      //foreach遍历每个对象
      var data = [
        {
          id: 1,
          name: "水果",
          goods: [
            {
              id: 11,
              gname: "苹果"
            },
            {
              id: 12,
              gname: "香蕉"
            }
          ]
        },
        {
          id: 2,
          name: "蔬菜",
          goods: [
            {
              id: 21,
              gname: "萝卜"
            },
            {
              id: 22,
              gname: "白菜"
            }
          ]
        }
      ];
      let obj = {};
      function getID(json, id) {
        /*      debugger */
        json.forEach(function(item) {
          if (item.id === id) {
            /*       console.log(item); */
            obj = item;
            /*    console.log(obj); */
            return item;
          } else if (item.goods && item.goods.length > 0) {
            obj = getID(item.goods, id);
          }
        });
        /*    console.log(obj); */
        return obj;
      }

      console.log(getID(data, 1));
      console.log(getID(data, 11));
      console.log(getID(data, 12));
      console.log(getID(data, 2));
      console.log(getID(data, 21));
      console.log(getID(data, 22));

      var obj1 = {
        id: 1,
        name: "andy",
        msg: {
          age: 18
        },
        color: ["red", "blue"]
      };
      var obj2 = {};
      //浅拷贝
      /*    for(var k in obj1){
         obj2[k]=obj1[k]
     } */
      // 浅拷贝 es6语法糖
      Object.assign(obj2, obj1);
      console.log(obj2);
      //深拷贝
      var obj3 = {};
      function deepCopy(newobj, oldobj) {
        for (var k in oldobj) {
          //判断属性值属于哪种数据类型
          var item = oldobj[k];
          if (item instanceof Array) {
            newobj[k] = [];
            deepCopy(newobj[k], item);
          } else if (item instanceof Object) {
            newobj[k] = {};
            deepCopy(newobj[k], item);
          } else {
            newobj[k] = item;
          }
        }
      }
      deepCopy(obj3, obj1);
      console.log(obj3);
    </script>
  </body>
</html>
